﻿using Taype.Trading.BTC.MtGoxAPIClient.JSONObjects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using Taype.Trading.BTC.MtGoxAPIClient;

namespace MtGoxAPIClient_test
{
    
    
    /// <summary>
    ///This is b test class for MtGoxOrderTest and is intended
    ///to contain all MtGoxOrderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MtGoxOrderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in b class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for MtGoxOrder Constructor
        ///</summary>
        [TestMethod()]
        public void MtGoxOrderConstructorTest()
        {
            MtGoxOrder target = new MtGoxOrder();
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for getObjects
        ///</summary>
        [TestMethod()]
        public void getObjectsMtGoxOrderTest0()
        {
            // first let's cancel any open test orders
            v0 target = new v0();
            List<MtGoxOrder> openOrders = target.getOrders(MtGoxOrderType.Buy, MtGoxOrderStatus.Invalid, 0);
            foreach(MtGoxOrder ord in openOrders)
            {
                target.cancelOrder(ord.oid, MtGoxOrderType.Buy);
            }
            openOrders = target.getOrders(MtGoxOrderType.Sell, MtGoxOrderStatus.Invalid, 0);
            foreach (MtGoxOrder ord in openOrders)
            {
                target.cancelOrder(ord.oid, MtGoxOrderType.Buy);
            }

            // if there are no open orders json data will look like this:
            // "{\"usds\":\"0\",\"btcs\":\"0\",\"new_usds\":{\"value\":\"0.00000\",\"value_int\":\"0\",\"display\":\"$0.00000\",\"currency\":\"USD\"},\"new_btcs\":{\"value\":\"0.00000000\",\"value_int\":\"0\",\"display\":\"0.00000000\\u00a0BTC\",\"currency\":\"BTC\"},\"orders\":[]}"
            
            string jsonDataStr = "{\"usds\":\"0\",\"btcs\":\"0\",\"new_usds\":{\"value\":\"0.00000\",\"value_int\":\"0\",\"display\":\"$0.00000\",\"currency\":\"USD\"},\"new_btcs\":{\"value\":\"0.00000000\",\"value_int\":\"0\",\"display\":\"0.00000000\\u00a0BTC\",\"currency\":\"BTC\"},\"orders\":[]}";
            List<MtGoxOrder> actual;
            actual = MtGoxOrder.getObjects(jsonDataStr);
            Assert.AreEqual(0, actual.Count);
        }

        /// <summary>
        ///A test for getObjects
        ///</summary>
        [TestMethod()]
        public void getObjectsMtGoxOrderTest1()
        {
            // if there are open orders json data will look like this:
            // "{\"status\":\"Your order has been queued for execution\",\"oid\":\"626c07b9-6f58-4665-b972-0c3f438b6ab4\",\"orders\":[{\"oid\":\"626c07b9-6f58-4665-b972-0c3f438b6ab4\",\"currency\":\"USD\",\"item\":\"BTC\",\"type\":2,\"amount\":\"0.01000000\",\"amount_int\":\"1000000\",\"price\":\"1.00000\",\"price_int\":\"100000\",\"status\":2,\"real_status\":\"pending\",\"dark\":0,\"date\":1325790759,\"priority\":\"1325790759344099\"}]}"
            string jsonDataStr = "{\"status\":\"Your order has been queued for execution\",\"oid\":\"626c07b9-6f58-4665-b972-0c3f438b6ab4\",\"orders\":[{\"oid\":\"626c07b9-6f58-4665-b972-0c3f438b6ab4\",\"currency\":\"USD\",\"item\":\"BTC\",\"type\":2,\"amount\":\"0.01000000\",\"amount_int\":\"1000000\",\"price\":\"1.00000\",\"price_int\":\"100000\",\"status\":2,\"real_status\":\"pending\",\"dark\":0,\"date\":1325790759,\"priority\":\"1325790759344099\"}]}";
            MtGoxOrder expected = new MtGoxOrder();
            expected.oid = "626c07b9-6f58-4665-b972-0c3f438b6ab4";
            expected.currency = MtGoxCurrencySymbol.USD;
            expected.item = MtGoxCurrencySymbol.BTC;
            expected.type = MtGoxOrderType.Buy;
            expected.amount = 0.01000000;
            expected.amount_int = 1000000;
            expected.price = 1.00000;
            expected.price_int = 100000;
            expected.status = MtGoxOrderStatus.Invalid;
            expected.real_status = "pending";
            expected.dark = 0;
            expected.date= 1325790759;
            expected.priority = 1325790759344099;
            List<MtGoxOrder> actual;
            actual = MtGoxOrder.getObjects(jsonDataStr);
            MtGoxOrder toFind = actual.Find(
                delegate(MtGoxOrder ord)
                {
                    return ord.oid == expected.oid;
                }
            );
            Assert.AreEqual(expected.amount, toFind.amount);
            Assert.AreEqual(expected.amount_int, toFind.amount_int);
            Assert.AreEqual(expected.currency, toFind.currency);
            Assert.AreEqual(expected.dark, toFind.dark);
            Assert.AreEqual(expected.date, toFind.date);
            Assert.AreEqual(expected.item, toFind.item);
            Assert.AreEqual(expected.oid, toFind.oid);
            Assert.AreEqual(expected.price, toFind.price);
            Assert.AreEqual(expected.price_int, toFind.price_int);
            Assert.AreEqual(expected.priority, toFind.priority);
            Assert.AreEqual(expected.real_status, toFind.real_status);
            Assert.AreEqual(expected.status, toFind.status);
            Assert.AreEqual(expected.type, toFind.type);
        }
    }
}
